package org.mango.validator;

import com.mango.validator.handler.UniqueValidator;
import com.mango.validator.util.ValidContext;
import org.mango.mapper.TagMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * Created by meigang on 17/10/21.
 */
@Component
public class TagNameValidator extends UniqueValidator {
    @Autowired
    private TagMapper mapper;

    @Override
    public boolean validate(UniqueValidator validator,String mapperMethod, Map<String, Object> sqlparam, ValidContext vc, String msg) {
        try {
            Method method = mapper.getClass().getMethod(mapperMethod,Map.class);
            Integer count = (Integer) method.invoke(mapper,sqlparam);
            if (count > 0) {
                //重复
                vc.addError("tag.name", msg);
                return false;
            }
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return true;
    }

}
